package ${model.packageName}.${model.modulePackage}.${model.modelL}.service;

import java.util.List; 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.mgr.common.utils.DateUtils;
import com.mgr.common.utils.security.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mgr.common.utils.StringUtils;
import ${model.packageName}.${model.modulePackage}.${model.modelL}.mapper.${model.model}Mapper;
import ${model.packageName}.${model.modulePackage}.${model.modelL}.domain.${model.model};
import ${model.packageName}.${model.modulePackage}.${model.modelL}.service.I${model.model}Service;
import com.mgr.common.constant.UserConstants;
import com.mgr.common.support.Convert;
/**
 * ${model.comment} 服务层实现
 * 
 * @author ${model.author}
 * @date ${date}
 */
@Service
public class ${model.model}ServiceImpl implements I${model.model}Service 
{
	private static final Logger log = LoggerFactory.getLogger(${model.model}ServiceImpl.class);
	@Autowired
	private ${model.model}Mapper ${model.modelL}Mapper;
	
	/**
     * 查询${model.comment}信息
     * 
     * @param id ${model.comment}ID
     * @return ${model.comment}信息
     */
	 @Override
	public ${model.model} select${model.model}ById(Long id)
	{
	    return ${model.modelL}Mapper.select${model.model}ById(id);
	}
	
	/**
     * 查询${model.comment}列表
     * 
     * @param ${model.modelL} ${model.comment}信息
     * @return ${model.comment}集合
     */
	 @Override
	public List<${model.model}> select${model.model}List(${model.model} ${model.modelL}){
	
	    return ${model.modelL}Mapper.select${model.model}List(${model.modelL});
	}
    /**
     * 查询${model.comment}集合
     * 
     * @return 所有${model.comment}信息
     */
    @Override
    public List<${model.model}> selectAll()
    {
        return ${model.modelL}Mapper.selectAll();
    }
    /**
     * 新增${model.comment}
     * 
     * @param ${model.modelL} ${model.comment}信息
     * @return 结果
     */
	 @Override
	public int insert${model.model}(${model.model} ${model.modelL}){
	 
		${model.modelL}.setCreateBy(ShiroUtils.getLoginName()); 
	    return ${model.modelL}Mapper.insert${model.model}(${model.modelL});
	}
	
	/**
     * 修改${model.comment}
     * 
     * @param ${model.modelL} ${model.comment}信息
     * @return 结果
     */
	 @Override
	public int update${model.model}(${model.model} ${model.modelL})
	{
		${model.modelL}.setUpdateBy(ShiroUtils.getLoginName()); 
	    return ${model.modelL}Mapper.update${model.model}(${model.modelL});
	}
	
	/**
     * 保存${model.comment}
     * 
     * @param ${model.modelL} ${model.comment}信息
     * @return 结果
     */
	 @Override
	public int save${model.model}(${model.model} ${model.modelL})
	{
	    Long id = ${model.modelL}.getId();
		int rows = 0;
		if (StringUtils.isNotNull(id)){
        	${model.modelL}.setUpdateBy(ShiroUtils.getLoginName());
			${model.modelL}.setUpdateTime(DateUtils.getNowDate());
		    rows = ${model.modelL}Mapper.update${model.model}(${model.modelL});
		}else{	
			${model.modelL}.setCreateTime(DateUtils.getNowDate());
			${model.modelL}.setCreateBy(ShiroUtils.getLoginName());
			${model.modelL}.setUpdateBy(ShiroUtils.getLoginName());
			${model.modelL}.setUpdateTime(DateUtils.getNowDate());
		    rows = ${model.modelL}Mapper.insert${model.model}(${model.modelL});
		}
		return rows;
	}
	
	/**
     * 删除${model.comment}信息
     * 
     * @param id ${model.comment}ID
     * @return 结果
     */
	 @Override
	public int delete${model.model}ByIds(String ids){
	
	    return ${model.modelL}Mapper.delete${model.model}ByIds(Convert.toStrArray(ids));
	}
	
	 
	
#foreach($field in $fieldList)
#if($field.existsMethod)
	/**
     * 校验${field.comment}是否唯一
     * 
     * @param ${field.field} ${field.comment}
     * @return
     */
    @Override
    public String check${field.fieldUp}Unique(String ${field.field},Long id){
    
        int count = ${model.modelL}Mapper.check${field.fieldUp}Unique(${field.field},id);
         if (count > 0){
            return UserConstants.CONFIG_KEY_NOT_UNIQUE;
        }
        return UserConstants.CONFIG_KEY_UNIQUE;
    }
#end
#if($field.findBy&&$field.field!="id")
	/**
     * 根据${field.comment}查询
     * 
     * @param ${field.field} ${field.comment}
     * @return
     */ 
     @Override
    public ${model.model} select${model.model}By${field.fieldUp}(String ${field.field})
    {
        return ${model.modelL}Mapper.select${model.model}By${field.fieldUp}(${field.field});
    }
#end
#end
}
